package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"wechat/models/mysql"
	"wechat/models/redis"

	"wechat/wechatrpc/internal/svc"
	"wechat/wechatrpc/wechatrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册
func (l *RegisterLogic) Register(in *wechatrpc.RegisterRequest) (*wechatrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Rdb.Get(l.ctx, "sendsms"+in.Account)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("短信验证码错误")
	}
	user := mysql.Wechat{
		Model:    gorm.Model{},
		Account:  in.Account,
		Password: in.Password,
	}
	mysql.DB.Create(&user)
	return &wechatrpc.RegisterResponse{
		Id: int64(user.ID),
	}, nil
}
